import { join } from 'path';
import { normalizePath } from 'vite';
import transformRoutes from './transform-routes';
export default function routerPlugin() {
  return {
    name: 'vite-plugin-router',
    async buildStart() {
      console.log('buildStart update router');
      transformRoutes();
    },
    async configureServer(devServer) {
      const { watcher } = devServer;

      watcher.on('change', async (eventName) => {
        const currentPath = normalizePath(eventName);
        const targetPath = normalizePath(
          join(process.cwd(), './src/routes.tsx'),
        );

        if (currentPath === targetPath) {
          console.log('configureServer update router');
          transformRoutes();
        }
      });
    },
  };
}
